import esigntool1
import requests
from esigntool1 import esign_run_print_outer
from esigntool1.esign_file import fileHelp

config = esigntool1.config()  # 初始化配置类

@esign_run_print_outer # 装饰器，打印函数执行时间
def FindOrgIdentityInfo(orgName):
    """
    查询机构认证信息
    :return:
    """
    api_path = "/v3/organizations/identity-info?orgName={}".format(orgName)
    method = esigntool1.httpMethodEnum.GET
    if orgName == "":
        print("请设置实名企业名称")
        exit()
    # 签名并构造签名鉴权json请求头
    json_headers = esigntool1.buildSignJsonHeader(config.appId, config.scert, method, esigntool1.apiPathSort(api_path))
    # 发起请求
    resp = requests.request(method, config.host + api_path, json=None, headers=json_headers)
    print(resp.text)
    orgId = resp.json()['data']['orgId']
    return orgId

@esign_run_print_outer # 装饰器，打印函数执行时间
def Signtemplates(orgIds):
    """
    获取模板列表
    :param func:
    :return:
    """
    api_path = "/v3/sign-templates?orgId={}".format(orgIds)
    print(api_path)
    method = esigntool1.httpMethodEnum.GET

        # 签名并构造签名鉴权json请求头
    json_headers = esigntool1.buildSignJsonHeader(
            config.appId,
            config.scert,
            method,
            esigntool1.apiPathSort(api_path)
        )
        # 发起请求
    resp = requests.request(method, config.host + api_path, json=None, headers=json_headers)
    print(resp.text)
    respx = resp.json()['data']['signTemplates']
    signTemplateId=resp.json()['data']['signTemplates'][0]['signTemplateId']
    signTemplateName=resp.json()['data']['signTemplates'][0]['signTemplateName']
    print(signTemplateId)
    print(signTemplateName)
    print(respx)
    return respx

@esign_run_print_outer # 装饰器，打印函数执行时间
def signtemplatesdetail(signTemplateId):
    """
     获取模板详情
     :param func:
     :return:
     """

    api_path = "/v3/sign-templates/detail?signTemplateId={}".format(signTemplateId)+"&orgId={}&queryComponents=true".format(config.orgId)
    print(api_path)
    method = esigntool1.httpMethodEnum.GET
    # 签名并构造签名鉴权json请求头
    json_headers = esigntool1.buildSignJsonHeader(
        config.appId,
        config.scert,
        method,
        esigntool1.apiPathSort(api_path)
    )
        # 发起请求
    resp = requests.request(method, config.host + api_path, json=None, headers=json_headers)
    print(resp.text)
    return resp

@esign_run_print_outer # 装饰器，打印函数执行时间
def create_by_sign_template(component_value,psn_value):
    """
    通过模板创建签署流程
    :component_value :发起方名字
    :psn_account_1:发起方手机号
    :psn_name_2:签合同的学员
    :psn_account_2:签合同的学员手机号
    :component_value= ["林晓彬","445222198908154037"]
    :psn_value=["林晓彬","17688862308","李四","15989116401"]
    """
    # 定义变量
    sign_template_id = "50cfda40908a42eead60893c9cabef45"
    sign_flow_title = "成人继续教育（学历）报读协议-{}".format(psn_value[2])
    print(sign_flow_title)
    auto_finish = True
    auto_start = True
    notice_types = "1"
    notify_url = "http://xx.xx.xx/notify"  # 请确保这是正确的URL 接收相关回调通知的Web地址
    #无需修改
    available_sign_client_types = "1,2" # 可用签署客户端类型，默认全部可用
    auto_fill_and_submit = True
    edit_component_value = False
    ############
    #参与人信息
    # 参与人信息 - 发起方
    psn_account_1 = psn_value[1]
    psn_name_1 = psn_value[0]
    # 参与人信息 - 签合同的学员
    psn_name_2 = psn_value[2] #姓名
    psn_account_2 = psn_value[3] #电话


    #预填信息
    component_value_1 = component_value[0]
    component_value_2 = component_value[1]
    file_id = "bbfcdf187f08477bbe13af3ab791c121"
    #预填信息 学员姓名
    component_id_1 = "a602f378ea424a81a4fb21f9baf91ae3"
    #预填信息 学员身份证
    component_id_2 = "3907712dbf9f4f26822b4ed1ea1c8116"
    # 构建字典
    sign_flow_config = {
        "signFlowTitle": sign_flow_title,
        "autoFinish": auto_finish,
        "autoStart": auto_start,
        "noticeConfig": {
            "noticeTypes":"1" # 通知类型，默认短信通知
        },
        "notifyUrl": notify_url,
        "signConfig": {
            "availableSignClientTypes": available_sign_client_types,
            "autoFillAndSubmit": auto_fill_and_submit,
            "editComponentValue": edit_component_value
        }
    }
    #参与人
    participants = [
        {
            "participantId": "8fe619fdf2b541ecb966ed8f8a4966c0",
            "psnParticipant": {
                "psnAccount": psn_account_1,
                "psnName": psn_name_1
            }
        },
        {
            "participantId": "ec5cfb922bab4252888c48e254257c11",
            "psnParticipant": {
                "psnAccount": psn_account_2,
                "psnName": psn_name_2
            }
        }
    ]
    #预填信息
    components = [
        {
            "fileId": file_id,
            "componentId": component_id_1,
            "componentValue": component_value_1
        },
        {
            "fileId": file_id,
            "componentId": component_id_2,
            "componentValue": component_value_2
        }
    ]
    # 将所有部分组合成一个大字典
    data = {
        "signTemplateId": sign_template_id,
        "signFlowConfig": sign_flow_config,
        "participants": participants,
        "components": components
    }
    # 打印出这个字典，以便查看
    print(data)
    api_path = "/v3/sign-flow/create-by-sign-template"
    print(api_path)
    method = esigntool1.httpMethodEnum.POST

    # 签名并构造签名鉴权json请求头
    json_headers = esigntool1.buildSignJsonHeader(config.appId, config.scert,
                                                  method, api_path, data)  # 签名并构造签名鉴权json请求头
    resp = requests.request(method, config.host + api_path, json=data, headers=json_headers)  # 发送请求
    print(resp.text)
    sqlstr=resp.json()['data']['signFlowId']
    print(sqlstr)
    return sqlstr

@esign_run_print_outer # 装饰器，打印函数执行时间
def sign_flow_draft_url(getsigntemplate_id,psn_account):
    """
    获取草稿链接
    :param signtemplate_id:
    :return:
    """
      # 请注意，实际使用时可能需要替换星号为具体的数字
    # 构建字典
    operator = {
        "psnAccount": psn_account
    }

    # 构建主字典
    data = {
        "operator": operator,
        "needLogin": False,
        "urlType": 2,
        "clientType": "ALL",
    }
    api_path = "/v3/sign-flow/{}/draft-url".format(getsigntemplate_id)
    method = esigntool1.httpMethodEnum.POST

    json_headers = esigntool1.buildSignJsonHeader(config.appId, config.scert,
                                                  method, api_path, data)  # 签名并构造签名鉴权json请求头
    resp = requests.request(method, config.host + api_path, json=data, headers=json_headers)  # 发送请求
    print(resp.text)
    url=resp.json()['data']['draftUrl']
    return url


###########################################################
orgName = "广州市天河区成科自学考试辅导中心"
# orgId_inf='bc4b12173a5c4c328123c0f16d986889'
# signTemplateId='50cfda40908a42eead60893c9cabef45'
component_value= ["林晓彬","445222198908154037"]
psn_value=["林晓彬","17688862308","李四","15989116401"]
# signtemplate_id=create_by_sign_template(component_value,psn_value)
# tx_tel=psn_value[1]
# sign_flow_draft_url(signtemplate_id,tx_tel)

import webbrowser
# 网页URL
# url = sign_flow_draft_url(create_by_sign_template(component_value,psn_value),psn_value[1])
# webbrowser.open_new_tab(url)

FindOrgIdentityInfo(orgName)

